package com.example.interviewrecorder.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.example.interviewrecorder.LyyBaseActivity
import com.example.interviewrecorder.LyyDatabase
import com.example.interviewrecorder.R
import com.example.interviewrecorder.adapter.RecordAdapter
import com.example.interviewrecorder.components.showInfo
import com.example.interviewrecorder.dao.RecordDao
import com.example.interviewrecorder.databinding.ActivityRecordDetailBinding
import com.example.interviewrecorder.model.Record
import com.example.interviewrecorder.utils.LyyActivityCollector
import com.example.interviewrecorder.utils.getFormatDateStr
import com.example.interviewrecorder.utils.showToast
import com.example.interviewrecorder.utils.toDetailDateTimeStr
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * @Description 记录详情页
 * @Author Yingyong Lao
 * @Date 2025/5/3 22:06
 */
class RecordDetailActivity : LyyBaseActivity(), View.OnClickListener {
    private val recordDao: RecordDao = LyyDatabase.getDatabase().getRecordDao()
    private lateinit var binding: ActivityRecordDetailBinding
    private var record: Record? = null

    companion object{
        fun start(record:Record){
            val context=LyyActivityCollector.getCurrActivity()
            val intent= Intent(context, RecordDetailActivity::class.java)
            intent.putExtra(RecordAdapter.RECORD_KEY,record)
            context.startActivity(intent)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityRecordDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        record = intent.getParcelableExtra(RecordAdapter.RECORD_KEY)
        record?.let {
            binding.titleTv.text = it.title
            binding.contentTv.text = it.content
            binding.corporateNameTv.text = it.corporateName
            binding.durationTv.text = it.duration
            binding.itvDateTv.text = it.itvDate.getFormatDateStr()
            binding.ratingBar.rating = it.rating.toFloat()
            binding.createdTimeTv.text = it.createdTime.toDetailDateTimeStr()
            binding.updatedTimeTv.text = it.updatedTime.toDetailDateTimeStr()
        }

        initEvents()
    }

    private fun initEvents() {
        binding.delBtn.setOnClickListener(this)
        binding.editBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.delBtn -> showDelDlg()
            R.id.editBtn -> jumpToEdit()
        }
    }

    private fun jumpToEdit() {
        record?.let {
            EditRecordActivity.start(it)
        }
    }

    private fun showDelDlg() {
        showInfo(
            title = getString(R.string.tip_title),
            content = getString(R.string.confirm_del_dlg_content)
        ) {
            lifecycleScope.launch {
                launch(Dispatchers.IO) {
                    repeatOnLifecycle(Lifecycle.State.STARTED) {
                        record?.let {
                            kotlin.runCatching {
                                recordDao.deleteRecord(it)
                            }.onSuccess {
                                runOnUiThread {
                                    showToast(getString(R.string.del_success_toast_text))
                                    finish()
                                }
                            }.onFailure {
                                runOnUiThread {
                                    showToast(getString(R.string.del_fail_toast_text))
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}